﻿namespace Dcms.OleDbDAL
{
    using Dcms.BaseLib;
    using Dcms.IDAL;
    using Dcms.Model;
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.OleDb;

    public class Dcms_ModelDAL : IDcms_ModelDAL
    {
        public Dcms_ModelModel AddModel(Dcms_ModelModel model)
        {
            string sQLString = "insert into Dcms_Model(Model_Name,Model_Type,Model_Order,Model_Url,Model_Intro,Model_Lang,Model_Level,Model_UseFlag) values(@Model_Name,@Model_Type,@Model_Order,@Model_Url,@Model_Intro,@Model_Lang,@Model_Level,@Model_UseFlag)";
            OleDbParameter[] cmdParms = new OleDbParameter[] { new OleDbParameter("@Model_Name", OleDbType.VarChar), new OleDbParameter("@Model_Type", OleDbType.VarChar), new OleDbParameter("@Model_Order", OleDbType.Integer), new OleDbParameter("@Model_Url", OleDbType.VarChar), new OleDbParameter("@Model_Intro", OleDbType.VarChar), new OleDbParameter("@Model_Lang", OleDbType.VarChar), new OleDbParameter("@Model_Level", OleDbType.Integer), new OleDbParameter("@Model_UseFlag", OleDbType.Integer) };
            model.Model_Order = this.GetNewOrder(model.Model_Type, model.Model_Lang);
            Dcms_ModelModel parentModel = new Dcms_ModelModel();
            parentModel = this.GetParentModel(model.Model_Type, model.Model_Lang);
            if (parentModel != null)
            {
                string str2 = parentModel.Model_Url.Split(new char[] { '.' })[0];
                model.Model_Url = str2.Substring(0, str2.Length - 1) + model.Model_Order + ".aspx";
                model.Model_Intro = parentModel.Model_Intro;
            }
            cmdParms[0].Value = model.Model_Name;
            cmdParms[1].Value = model.Model_Type;
            cmdParms[2].Value = model.Model_Order;
            cmdParms[3].Value = model.Model_Url;
            cmdParms[4].Value = model.Model_Intro;
            cmdParms[5].Value = model.Model_Lang;
            cmdParms[6].Value = model.Model_Level;
            cmdParms[7].Value = model.Model_UseFlag;
            model.Model_ID = OleDbHelper.ExecuteSql(sQLString, cmdParms);
            return model;
        }

        public int CheckModel(string Name, string Type, string lang)
        {
            string sQLString = "select top 1 * from Dcms_Model where Model_Name=@Model_Name and Model_Type=@Model_Type and Model_Lang=@Model_Lang";
            OleDbParameter[] cmdParms = new OleDbParameter[] { new OleDbParameter("@Model_Name", OleDbType.VarChar), new OleDbParameter("@Model_Type", OleDbType.VarChar), new OleDbParameter("@Model_Lang", OleDbType.VarChar) };
            cmdParms[0].Value = Name;
            cmdParms[1].Value = Type;
            cmdParms[2].Value = lang;
            DataSet set = OleDbHelper.CreateDataSet(sQLString, cmdParms);
            if (set.Tables["ds"].Rows.Count > 0) return Convert.ToInt32(set.Tables["ds"].Rows[0]["Model_Order"]);
            return -1;
        }

        public IList<Dcms_ModelModel> DataSetToList(DataSet ds)
        {
            IList<Dcms_ModelModel> list = new List<Dcms_ModelModel>();
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                Dcms_ModelModel item = new Dcms_ModelModel();
                if (ds.Tables[0].Rows[i]["Model_ID"].ToString() != "") item.Model_ID = int.Parse(ds.Tables[0].Rows[i]["Model_ID"].ToString());
                if (ds.Tables[0].Rows[i]["Model_Order"].ToString() != "") item.Model_Order = int.Parse(ds.Tables[0].Rows[i]["Model_Order"].ToString());
                if (ds.Tables[0].Rows[i]["Model_Level"].ToString() != "") item.Model_Level = int.Parse(ds.Tables[0].Rows[i]["Model_Level"].ToString());
                if (ds.Tables[0].Rows[i]["Model_UseFlag"].ToString() != "") item.Model_UseFlag = int.Parse(ds.Tables[0].Rows[i]["Model_UseFlag"].ToString());
                item.Model_Name = ds.Tables[0].Rows[i]["Model_Name"].ToString();
                item.Model_Type = ds.Tables[0].Rows[i]["Model_Type"].ToString();
                item.Model_Url = ds.Tables[0].Rows[i]["Model_Url"].ToString();
                item.Model_Intro = ds.Tables[0].Rows[i]["Model_Intro"].ToString();
                item.Model_Lang = ds.Tables[0].Rows[i]["Model_Lang"].ToString();
                list.Add(item);
            }
            return list;
        }

        public void DeleteModelByID(int ID)
        {
            string sQLString = "delete from Dcms_Model where Model_ID=@Model_ID";
            OleDbParameter[] cmdParms = new OleDbParameter[] { new OleDbParameter("@Model_ID", OleDbType.Integer) };
            cmdParms[0].Value = ID;
            OleDbHelper.ExecuteSql(sQLString, cmdParms);
        }

        public bool DelNewModel()
        {
            try
            {
                string sQLString = "delete  from  Dcms_Model where Model_Type <> 'SYSTEM' and   Model_Type <> 'CateSYSTEM' and Model_Order<>0";
                OleDbParameter[] cmdParms = new OleDbParameter[] { new OleDbParameter("@1", OleDbType.VarChar) };
                cmdParms[0].Value = 1;
                return OleDbHelper.ExecuteSql(sQLString, cmdParms) > 0;
            }
            catch
            {
                return false;
            }
        }

        public DataSet GetList(string sqlwhere)
        {
            string sql = "select * from  Dcms_Model";
            if (sqlwhere != "") sql = sql + " where " + sqlwhere;
            return OleDbHelper.CreateDataSet(sql);
        }

        public Dcms_ModelModel GetModel(string Name, string type, string lang)
        {
            string sQLString = "select * from Dcms_Model where Model_Name=@Model_Name and  Model_Type=@Model_Type and  Model_Lang=@Model_Lang ";
            OleDbParameter[] cmdParms = new OleDbParameter[] { new OleDbParameter("@Model_Name", OleDbType.VarChar), new OleDbParameter("@Model_Type", OleDbType.VarChar), new OleDbParameter("@Model_Lang", OleDbType.VarChar) };
            cmdParms[0].Value = Name;
            cmdParms[1].Value = type;
            cmdParms[2].Value = lang;
            DataSet ds = OleDbHelper.CreateDataSet(sQLString, cmdParms);
            IList<Dcms_ModelModel> list = this.DataSetToList(ds);
            return ((list.Count > 0) ? list[0] : null);
        }

        public Dcms_ModelModel GetModelByFalyOrder(string Faly, int order)
        {
            string sQLString = "select * from Dcms_Model where Model_Type=@Model_Type and  Model_Order=@Model_Order ";
            OleDbParameter[] cmdParms = new OleDbParameter[] { new OleDbParameter("@Model_Type", OleDbType.VarChar), new OleDbParameter("@Model_Order", OleDbType.Integer) };
            cmdParms[0].Value = Faly;
            cmdParms[1].Value = order;
            DataSet ds = OleDbHelper.CreateDataSet(sQLString, cmdParms);
            IList<Dcms_ModelModel> list = this.DataSetToList(ds);
            return ((list.Count > 0) ? list[0] : null);
        }

        public Dcms_ModelModel GetModelByID(int id)
        {
            string sQLString = "select * from Dcms_Model where Model_ID=@Model_ID";
            OleDbParameter[] cmdParms = new OleDbParameter[] { new OleDbParameter("@Model_Name", OleDbType.Integer) };
            cmdParms[0].Value = id;
            DataSet ds = OleDbHelper.CreateDataSet(sQLString, cmdParms);
            IList<Dcms_ModelModel> list = this.DataSetToList(ds);
            return ((list.Count > 0) ? list[0] : null);
        }

        public IList<Dcms_ModelModel> GetModelList(string sqlwhere)
        {
            return this.DataSetToList(this.GetList(sqlwhere));
        }

        public int GetNewOrder(string Type, string lang)
        {
            string sQLString = "select max(Model_Order) from Dcms_Model  where Model_Type=@Model_Type and Model_Lang=@Model_Lang";
            OleDbParameter[] cmdParms = new OleDbParameter[] { new OleDbParameter("@Model_Type", OleDbType.VarChar), new OleDbParameter("@Model_Lang", OleDbType.VarChar) };
            cmdParms[0].Value = Type;
            cmdParms[1].Value = lang;
            object single = OleDbHelper.GetSingle(sQLString, cmdParms);
            if (single != null) return Convert.ToInt32(single) + 1;
            return -1;
        }

        public int GetOrder(string Name, string type, string lang)
        {
            string sQLString = "select Model_Order from Dcms_Model where Model_Name=@Model_Name and Model_Type=@Model_Type and Model_Lang=@Model_Lang";
            OleDbParameter[] cmdParms = new OleDbParameter[] { new OleDbParameter("@Model_Name", OleDbType.VarChar), new OleDbParameter("@Model_Type", OleDbType.VarChar), new OleDbParameter("@Model_Lang", OleDbType.VarChar) };
            cmdParms[0].Value = Name;
            cmdParms[1].Value = type;
            cmdParms[2].Value = lang;
            object single = OleDbHelper.GetSingle(sQLString, cmdParms);
            if (single != null) return int.Parse(single.ToString());
            return -1;
        }

        public Dcms_ModelModel GetParentModel(string Type, string lang)
        {
            if (Type != "SYSTEM" && Type != "CateSYSTEM")
            {
                string sQLString = "select * from Dcms_Model where Model_Type=@Model_Type and Model_Order=0 and  Model_Lang=@Model_Lang ";
                OleDbParameter[] cmdParms = new OleDbParameter[] { new OleDbParameter("@Model_Type", OleDbType.VarChar), new OleDbParameter("@Model_Lang", OleDbType.VarChar) };
                cmdParms[0].Value = Type;
                cmdParms[1].Value = lang;
                DataSet ds = OleDbHelper.CreateDataSet(sQLString, cmdParms);
                IList<Dcms_ModelModel> list = this.DataSetToList(ds);
                return ((list.Count > 0) ? list[0] : null);
            }
            return null;
        }

        public string GetUrl(string Type, int order)
        {
            string sQLString = "select Model_Url from Dcms_Model where Model_Type=@Model_Type and Model_Order=@Model_Order";
            OleDbParameter[] cmdParms = new OleDbParameter[] { new OleDbParameter("@Model_Name", OleDbType.VarChar), new OleDbParameter("@Model_Order", OleDbType.Integer) };
            cmdParms[0].Value = Type;
            cmdParms[1].Value = order;
            object single = OleDbHelper.GetSingle(sQLString, cmdParms);
            if (single != null) return single.ToString();
            return null;
        }

        public Dcms_ModelModel Update(Dcms_ModelModel model)
        {
            string sQLString = "update Dcms_Model set Model_Name=@Model_Name,Model_Type=@Model_Type, Model_Order=@Model_Order,Model_Url=@Model_Url,Model_Intro=@Model_Intro,Model_Lang=@Model_Lang,Model_Level=@Model_Level,Model_UseFlag=@Model_UseFlag where Model_ID=@Model_ID";
            OleDbParameter[] cmdParms = new OleDbParameter[] { new OleDbParameter("@Model_Name", OleDbType.VarChar), new OleDbParameter("@Model_Type", OleDbType.VarChar), new OleDbParameter("@Model_Order", OleDbType.Integer), new OleDbParameter("@Model_Url", OleDbType.VarChar), new OleDbParameter("@Model_Intro", OleDbType.VarChar), new OleDbParameter("@Model_Lang", OleDbType.VarChar), new OleDbParameter("@Model_Level", OleDbType.Integer), new OleDbParameter("@Model_UseFlag", OleDbType.Integer), new OleDbParameter("@Model_ID", OleDbType.Integer) };
            cmdParms[0].Value = model.Model_Name;
            cmdParms[1].Value = model.Model_Type;
            cmdParms[2].Value = model.Model_Order;
            cmdParms[3].Value = model.Model_Url;
            cmdParms[4].Value = model.Model_Intro;
            cmdParms[5].Value = model.Model_Lang;
            cmdParms[6].Value = model.Model_Level;
            cmdParms[7].Value = model.Model_UseFlag;
            cmdParms[8].Value = model.Model_ID;
            OleDbHelper.ExecuteSql(sQLString, cmdParms);
            return model;
        }
    }
}

